<?php
session_start() ;
{$_SESSION[ "service_dir" ]  = str_replace("\\","/",getcwd()) ;} ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "ByteBuffer.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "DataUtil.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "Result.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "Session.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "UtilHash.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "UtilKey.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "phpmailer/class.phpmailer.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "phpmailer/class.smtp.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "PWM/StrUtility.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "DataApp.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "DataAppManager.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "DataArea.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "DataAreasManager.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "DataFbToken.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "DataFriends.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "DataGroup.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "DataGroupUsers.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "DataGroupsManager.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "DataGuest.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "DataUserGroups.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "DataUserCertificates.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "DataUserRegister.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "DataWaitingConfirm.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "FunctionApp.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "FunctionArea.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "FunctionData.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "FunctionGroup.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "FunctionSession.php"  )  ;
require_once( $_SESSION[ "service_dir" ]  .'/../' . "FunctionTransaction.php"  )  ;
function readPattern($file_name){
	$file = fopen($file_name,r);
	$line=false;
	if($file){
		$line = fgets($file);
		while (!feof($file))$line .= fgets($file);
	}
	{ $ptn  = new Util\str\Pattern( $line );} ;
	return $ptn;
}
function callService($service_name,$arg){
	$appid = $arg["appid"];
	if($appid == 0 || $appid == null || $appid == ""){
		if($service_name == null || $service_name == "") $service_name = "mainpage";
		$service_file = $_SESSION[ "service_dir" ]  .'/pattern/' . ('svc_'.  $service_name  ) .'.php' ;
	}else{
		if($service_name == null || $service_name == "") $service_name = FunctionApp::getAppDirectory($appid);
		$service_file = $_SESSION[ "service_dir" ]  .'/../Application/' .FunctionApp::getAppDirectory(  $appid  ).'/' . ('svc_'.  $service_name  ) .'.php' ;
	}
	if(file_exists($service_file)){
		require_once $service_file;
		$service_class = ('svc_'. $service_name ) ;
		$request = $arg["request"];
		$service = new $service_class($arg);
		return $service->getPublish();
	}else{
	}
	$rs = new Result();
	{Error::getInstance() ->addError( 0 , "UNKNOWN SERVICE" );} ;
	return  $rs ->publish(  ) ;
}
$method = trim($_REQUEST["method"]);
$lang = trim($_REQUEST["lang"]);
if(strlen($lang)<=0)
{
	$lang = "html";
}
if(strlen($method) > 0){
	$arg = array();
	if(is_array($_REQUEST)){
		foreach($_REQUEST as $key=>$value){
			if($key != "method" && $key != "PHPSESSID" && $key != "outh"){
				$arg[$key] = $value;
			}
		}
	}
	if($method == "service"){
		$id = $_REQUEST["sid"];
		$request = $_REQUEST["request"];
		if($request == "getForm"){
			$id = $_REQUEST["form_id"];
			$appid = $_REQUEST["appid"];
			$frm_file = '';
			if($appid == null || $appid =="" || $appid == 0){
				$frm_file = $_SESSION[ "service_dir" ]  .'/pattern/' . ('frm_'.  $id  ) .'.php' ;
			}else{
				$service_name = FunctionApp::getAppDirectory($appid);
				require_once( $_SESSION[ "service_dir" ]  .'/../Application/' .FunctionApp::getAppDirectory(  $appid  ).'/' . ('svc_'.  $service_name  ) .'.php'  ) ;
				$frm_file = $_SESSION[ "service_dir" ]  .'/../Application/' .FunctionApp::getAppDirectory(   $appid   ).'/' .'pattern/' . ('frm_'.  $id  ) .'.php' ;				
			}
			if(file_exists($frm_file)){
				require_once( $frm_file ) ;
				$frm_class = ('frm_'. $id ) ;
				$frm = new $frm_class($arg);
				$rs = new Result();
				$rs->addData("request",$request.":".$id);
				$rs->addData("content",$frm->getPublish());
				echo  $rs ->publish( "xml" ) ;
			}else{
			}
		}else{
			echo callService($id,$arg);
		}
	}
}
?>